package com.threeti.mecool.mobile.interfaces;

import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;


public class ResponserDto implements Serializable {
  private String command;
  private String result;
  private Map<String, Object> body;
  
  public ResponserDto(String command) {
    this.command = command;
  }
  
  public ResponserDto(String command, String result) {
    this(command);
    this.result = result;
  }
  
  public ResponserDto(String command, String result, Map<String, Object> body) {
    this(command, result);
    this.body = body;
  }
  
  public Map<String, Object> getBody() {
    return Collections.unmodifiableMap(body);
  }
  
  public String getCommand() {
    return command;
  }

  public String getResult() {
    return result;
  }

  Map<String, Object> getMutableBody() {
    return body == null ? (body = new LinkedHashMap<String, Object>()) : body;
  }

  /** 快速创建ResponseDto对象 */
  public static ResponserDto successCommand(String command) {
    return new ResponserDto(command, "1");
  }
  
  /** 快速记录业务级别的成功操作(内含需要返回的对象)  */
  public ResponserDto withBizSuccessBody(String bodyIdentifier, Object body) {
    return withBizSuccessBody(bodyIdentifier, body, BodyType.ENTITY);
  }
  
  /** 快速记录业务级别的成功操作(内含需要返回的对象)  */
  public ResponserDto withBizSuccessBody(String bodyIdentifier, Object body, BodyType bodyObjectType, long... pageIndexAndSize) {
    getMutableBody().put("status", "1");
    getMutableBody().put("type", String.valueOf(bodyObjectType.ordinal()));
    
    if (bodyObjectType == BodyType.PLIST) {
      getMutableBody().put("page", new PageResponseDto(pageIndexAndSize[0], (pageIndexAndSize[1]==1?true:false), (int)pageIndexAndSize[2]));
    }
    getMutableBody().put(bodyIdentifier, body);
    return this;
  }
  
  /** 快速记录业务级别的成功操作(内含需要返回的消息对象)  */
  public ResponserDto withMsgAttachement(String bodyIdentifier, Object body, BodyType bodyObjectType, long... pageIndexAndSize) {
    Map<String, Object> attachementMap = new LinkedHashMap<String, Object>();
    attachementMap.put("type", String.valueOf(bodyObjectType.ordinal()));    
    if (bodyObjectType == BodyType.PLIST) {
      attachementMap.put("page", new PageResponseDto(pageIndexAndSize[0], (pageIndexAndSize[1]==1?true:false), (int)pageIndexAndSize[2]));
    }
    attachementMap.put(bodyIdentifier, body);
    this.getMutableBody().put("attachment", attachementMap);
    return this;
  }
  
  /** 快速记录业务级别的成功操作(内含需要返回的分页列表对象)  */
  public ResponserDto withBizSuccessPageBody(String bodyIdentifier, Object body, PageResponseDto pageInfo) {
    return withBizSuccessBody(bodyIdentifier, body, BodyType.PLIST, pageInfo.getTotalElement(),pageInfo.isHasNext()?1:0, pageInfo.getPage());
  }
  
  /** 快速记录业务级别的成功消息 */
  public static ResponserDto withBizSuccessMsg(String command, String msgCode, String successMsgContent) {
    return 
        with(command, "1").//1表示不论业务操作是成功或者失败，都视为和服务器交付成功(一般即指服务器返回类似200的状态码)
        withBizSuccessBody("status", "1").
        withMsg(new ResponseMessageDto(msgCode, successMsgContent));
  }
  
  /** 快速记录业务级别的错误消息 */
  public static ResponserDto withBizFailureMsg(String command, String msgCode, String failureMsgContent) {
    return 
        with(command, "1").//1表示不论业务操作是成功或者失败，都视为和服务器交付成功(一般即指服务器返回类似200的状态码)
        withBizFailureBody("status", "0").
        withMsg(new ResponseMessageDto(msgCode, failureMsgContent));
  }
  
  /** 快速记录业务级别的成功消息 */
  public static ResponserDto withBizSuccessMsg(String command, String... successMsgPairs) {
    return 
        with(command, "1").//1表示不论业务操作是成功或者失败，都视为和服务器交付成功(一般即指服务器返回类似200的状态码)
        withBizSuccessBody("status", "1").
        withMsg(new ResponseMessageDto(successMsgPairs[0], successMsgPairs[1]));
  }
  
  /** 快速记录业务级别的错误消息 */
  public static ResponserDto withBizFailureMsg(String command, String... failureMsgPairs) {
    return 
        with(command, "1").//1表示不论业务操作是成功或者失败，都视为和服务器交付成功(一般即指服务器返回类似200的状态码)
        withBizFailureBody("status", "0").
        withMsg(new ResponseMessageDto(failureMsgPairs[0], failureMsgPairs[1]));
  }

  /** 快速创建ResponseDto对象 */
  private static ResponserDto with(String command, String result) {
    return new ResponserDto(command, result);
  }
  
  /** 快速记录业务级别的失败操作(内含需要返回的对象)  */
  private ResponserDto withBizFailureBody(String bodyIdentifier, Object body) {
    getMutableBody().put("status", "0");
    getMutableBody().put(bodyIdentifier, body);
    return this;
  }
  
  /** 记录响应消息内容 */
  private ResponserDto withMsg(ResponseMessageDto msgObject) {
    getMutableBody().put("msg", msgObject);
    return this;
  }
  
  /** 记录响应消息附件  */
//  private ResponserDto withAttachement(ResponseMessageDto msgObject) {
//    getMutableBody().put("attachement", msgObject);
//    return this;
//  }
  
}
